package com.chagee.store.operation.server.component;

import com.alibaba.fastjson.JSON;
import com.chagee.store.operation.server.entity.TaskExecutionDO;
import com.chagee.store.operation.server.event.service.BasicTaskConfigurationService;
import com.chagee.store.operation.server.event.service.StoreTaskRelationBaseService;
import com.chagee.store.operation.server.event.service.TaskExecutionBaseService;
import com.chagee.store.operation.server.event.service.WorkOrderBaseService;
import com.chagee.store.operation.server.hadler.StoreIssueNotice;
import com.chagee.store.operation.server.model.param.StoreIssueNoticeParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author : hk
 * @Date : 2024/6/3
 */

@Slf4j
@Component
@Configuration
public class StoreIssueNoticeComponent {

	@Resource
	private TaskExecutionBaseService taskExecutionBaseService;
	@Resource
	private StoreTaskRelationBaseService storeTaskRelationBaseService;
	@Resource
	private BasicTaskConfigurationService basicTaskConfigurationService;
	@Resource
	private WorkOrderBaseService workOrderBaseService;


	private static StoreIssueNoticeParam param;
	private static String appId;
	private static String h5Url;
	private static String domain;
	private static String lkTargetUrl;
	private static String pcDomain;
	private static String pcUrl;

	public StoreIssueNoticeComponent() {
	}

	public StoreIssueNoticeComponent(StoreIssueNoticeParam param, String appId, String h5Url, String domain, String lkTargetUrl, String pcDomain, String pcUrl, TaskExecutionBaseService taskExecutionBaseService) {
		this.param = param;
		this.appId = appId;
		this.h5Url = h5Url;
		this.domain = domain;
		this.lkTargetUrl = lkTargetUrl;
		this.pcDomain = pcDomain;
		this.pcUrl = pcUrl;
		this.taskExecutionBaseService = taskExecutionBaseService;
	}

	public StoreIssueNotice issueNotice() {
		TaskExecutionDO executionDO = taskExecutionBaseService.getById(param.getId());
		StoreIssueNotice notice = new StoreIssueNotice();

		String target_url = String.format(lkTargetUrl, domain, param.getId());
		log.info("lkTargetUrl加密前{}", target_url);

		try {
			String encode = URLEncoder.encode(target_url, "UTF-8");
			log.info("lkTargetUrl加密后{}", JSON.toJSON(encode));
			String h5Url = String.format(StoreIssueNoticeComponent.h5Url, appId, encode);
			notice.setH5Url(h5Url);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			log.info("路径lkTargetUrl加密失败{}", JSON.toJSON(target_url), e);
		}

		notice.setStoreNo(param.getStoreNo());
		notice.setIssueType(param.getIssueType());
		notice.setIssueDescription(param.getIssueDescription());
		String pcUrl = String.format(StoreIssueNoticeComponent.pcUrl, pcDomain, executionDO.getWorkOrderNumber());
		notice.setPcUrl(pcUrl);
		notice.setExtData(param.getExtData());
		return notice;
	}

	public static StoreIssueNoticeComponent notice(StoreIssueNoticeParam issueNoticeParam, String appId,
												   String h5Url, String domain, String lkTargetUrl, String pcDomain, String pcUrl, TaskExecutionBaseService taskExecutionBaseService) {
		StoreIssueNoticeComponent component = new StoreIssueNoticeComponent(issueNoticeParam, appId, h5Url, domain, lkTargetUrl, pcDomain, pcUrl, taskExecutionBaseService);
		return component;
	}
}
